﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Oracle.DataAccess.Client;
using System.Windows.Forms;
using System.Data;

namespace Election
{

    public class orcl
    {
        public static OracleConnection conn;
        public static string user;
        public static string pass;
		public static string imagePath = "image\\";


        ~orcl()
        {
            try
            {
                conn.Close();
            }
            catch
            {
            }
        }
		
        #region ExecuteQuery
        public static DataTable ExecuteQuery(String query)
        {

            DataTable table = new DataTable();
            try
            {
                OracleConnection connect = orcl.conn;
                try
                {
                    OracleCommand command = connect.CreateCommand();
                    command.CommandType = CommandType.Text;
                    command.CommandText = query;
                    OracleDataAdapter adapter = new OracleDataAdapter();
                    adapter.SelectCommand = command;
                    adapter.Fill(table);
                }

                catch (OracleException ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

            catch (Exception ex)
            {
                throw ex;
            }
            return table;
        }
        #endregion

        #region ExecuteNoneQuery
        public static int ExecuteNoneQuery(String query)
        {
            int n;
            try
            {

                OracleConnection connect = orcl.conn;
                try
                {
                    OracleCommand command = connect.CreateCommand();
                    command.CommandType = CommandType.Text;
                    command.CommandText = query;
                    n = command.ExecuteNonQuery();
                    return n;
                }

                catch (OracleException ex)
                {
                    MessageBox.Show(ex.Message);
                    return -2;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return -2;
            }
        }


        #endregion

        #region CheckRoleOfUser
        public static string checkRoleOfUser()
        {
            DataTable dt = new DataTable();

            dt = orcl.ExecuteQuery("select GRANTED_ROLE  from user_role_privs");
            string role = "NGUOIDIBAU";

            for(int i = 0; i < dt.Rows.Count; i++)
            {
                if(dt.Rows[i][0].ToString() != "NGUOIDIBAU")
                    return dt.Rows[i][0].ToString();
            }                    
            
            return role ;
        }
		#endregion
		
		

		#region ExecuteQuery
    public static DataTable ExecuteQueryThrowEx(String query)
    {
        DataTable table = new DataTable();
        try
        {
            OracleConnection connect = orcl.conn;
            try
            {
                OracleCommand command = connect.CreateCommand();
                command.CommandType = CommandType.Text;
                command.CommandText = query;
                OracleDataAdapter adapter = new OracleDataAdapter();
                adapter.SelectCommand = command;
                adapter.Fill(table);
            }
            catch (OracleException ex)
            {
                throw ex;
            }
        }

        catch (Exception ex)
        {
            throw ex;
        }
        return table;
    }
    #endregion

    #region ExecuteNoneQuery
    public static int ExecuteNoneQueryThrowEx(String query)
    {
        int n;
        try
        {
            OracleConnection connect = orcl.conn;
            try
            {
                OracleCommand command = connect.CreateCommand();
                command.CommandType = CommandType.Text;
                command.CommandText = query;
                n = command.ExecuteNonQuery();
                return n;
            }

            catch (OracleException ex)
            {
                throw ex;
            }            
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    #endregion

    public static void ExecuteStoredProcedure(String procName, OracleParameter[] param)
    {
        try
        {
            OracleConnection connect = orcl.conn;
            try
            {
                OracleCommand command = connect.CreateCommand();
                command.CommandType = CommandType.StoredProcedure;
                command.CommandText = procName;
                for (int i = 0; i < param.Length; i++)
                {
                    command.Parameters.Add(param[i]);
                }
                command.ExecuteNonQuery();
            }

            catch (OracleException ex)
            {
                throw ex;
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }


       

    }
}